package com.example.forum.controller;

import com.example.forum.domain.Message;
import com.example.forum.domain.Page;
import com.example.forum.domain.User;
import com.example.forum.service.MessageService;
import com.example.forum.service.UserService;
import com.example.forum.utils.HostHolder;
import com.example.forum.utils.SimpleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LetterController {
    @Autowired
    private MessageService messageService;
    @Autowired
    private UserService userService;
    @Autowired
    private HostHolder hostHolder;

    @RequestMapping("/letter")
    public String letter(Model model,@RequestParam(value = "curPage",required = false,defaultValue = "1")int curPage,
                         @RequestParam(value = "pageSize",required = false,defaultValue = "10")int pageSize){
        Map<String,List<Message>> map = messageService.findByFromToId(hostHolder.getUser().getId());
        SimpleUtils.subMap(map,(curPage-1)*pageSize,pageSize);
        List<Map<String,Object>> list=new ArrayList<>();
        for(String key:map.keySet()){
            Map<String,Object> map1=new HashMap<>();
            map1.put("lastMsg",map.get(key).get(map.get(key).size()-1));
            //得到与当前用户对话的用户id
            String[] ids = key.split("[_]");
            int userId=0;
            for(String id:ids){
                if(Integer.parseInt(id)!=hostHolder.getUser().getId()){
                    userId=Integer.parseInt(id);
                }
            }
            map1.put("user",userService.findById(userId));
            //查询两个用户会话的次数
            int count=messageService.findCount(hostHolder.getUser().getId(),userId);
            map1.put("count",count);
            //查询消息的未读条数
            int unreadCount=messageService.findUnreadCount(userId,hostHolder.getUser().getId());
            map1.put("unreadCount",unreadCount);
            list.add(map1);
        }
        model.addAttribute("msgsVo",list);
        //该用户总的未读消息条数
        int totalUnreadCount=messageService.totalUnreadCount(hostHolder.getUser().getId());
        model.addAttribute("totalUnreadCount",totalUnreadCount);
        Page page=new Page();
        page.setPageSize(pageSize);
        page.setCurPage(curPage);
        page.setTotalCount(map.size());
        model.addAttribute("page",page);
        return "site/letter";
    }

    @RequestMapping("/letter-detail")
    public String letterDetail(@RequestParam(name = "id2",required = true)int id2,Model model){
        List<Message> messages = messageService.findAllSession(hostHolder.getUser().getId(), id2);
        model.addAttribute("messages",messages);
        User user = userService.findById(id2);
        model.addAttribute("friend",user);
        return "site/letter-detail";
    }
}
